<?php
class o2o_ctl_admin_refuse_reason extends desktop_controller
{
    function index()
    {
        $this->finder('o2o_mdl_refuse_reason',array(
                'title'=>'门店拒单原因',
                'actions'=>array(
                        array(
                                'label'=>'添加',
                                'href'=>'index.php?app=o2o&ctl=admin_refuse_reason&act=add',
                                'target' => 'dialog::{width:450,height:150,title:\'新建原因\'}'
                        ),
                ),
                'use_buildin_new_dialog' => false,
                'use_buildin_set_tag'=>false,
                'use_buildin_recycle'=>false,
                'use_buildin_export'=>false,
                'use_buildin_import'=>false,
        ));
    }
    
    /**
     * 添加类型
     */
    function add()
    {
        $refuseObj    = app::get('o2o')->model('refuse_reason');
        if($_POST)
        {
            $this->begin('index.php?app=ome&ctl=refuse_reason&act=index');
            $refuseObj->save($_POST['reason']);
            $this->end(true, app::get('base')->_('保存成功'),3);
        }
        
        $this->pagedata['title'] = '添加门店拒单原因';
        $this->page("admin/system/reason.html");
    }
    
    /**
     * 编辑类型
     */
    function edit($reason_id)
    {
        $refuseObj    = app::get('o2o')->model('refuse_reason');
        $this->pagedata['reason']    = $refuseObj->dump($reason_id);
        $this->pagedata['title']       = '编辑门店拒单原因';
        $this->page("admin/system/reason.html");
    }

    /**
     * 拒单原因统计报表
     */
    function statistics(){
        if(empty($_POST)){
            $date_arr = $this->get_time_start();
            $_POST['time_from'] = $date_arr['now_zero_year'];
            $_POST['time_to'] = $date_arr['now_end_year'];
        }
        kernel::single('o2o_refuse_reason_statistics')->set_params($_POST)->display();
    }

    function get_time_start(){
        $t   =   time();   
        $nowday   =   date('Y-m-d',mktime(0,0,0,date("m",$t),date("d",$t),date("Y",$t)));            //当天零点的时间戳 
        $nowmonth   =   date('Y-m-d',mktime(0,0,0,date("m",$t),1,date("Y",$t)));                    //当月一号零点的时间戳
        $nextmonth =  date('Y-m-d',mktime(0,0,0,date("m",$t)+1,1,date("Y",$t)));                    //下月一号零点时间戳
        $nowyear    =   date('Y-m-d',mktime(0,0,0,1,1,date("Y",$t)));                                //当年一月一号零点时间戳
        $lastmonth =  date('Y-m-d',mktime(0,0,0,date("m",$t)-1,1,date("Y",$t)));                    //上月一号零点时间戳
        $nowendmonth = date('Y-m-d',mktime(0,0,0,date("m",$t)+1,1,date("Y",$t))-24*60*60);
        $nowendyear = date('Y-m-d',mktime(0,0,0,12,31,date("Y",$t)));
        return array('now_zero_day' => $nowday,
                     'now_zero_month'=>$nowmonth,
                     'now_zero_year'=>$nowyear,
                     'next_zero_month'=>$nextmonth,
                     'last_zero_month'=>$lastmonth,
                     'now_end_month'=>$nowendmonth,
                     'now_end_year'=>$nowendyear
        );
    }

    public function chart_view() {
        $filter = $_GET;
        if(isset($filter['report']) && $filter['report']=='month'){
            $time_from = strtotime($filter['time_from']);
            if($time_from == strtotime(date("Y-m-d"),time())){
                $time_from = ($time_from - 86400);
            }
            $last_time = date("Y-m-d",($time_from - 86400));
            $filter['time_from'] = date("Y-m-d",$time_from);
            $filter['time_to'] = date("Y-m-d",$filter['time_to']);
        }
        
        $filter = array(
            'time_from' => $filter['time_from'],
            'time_to' => $filter['time_to'],
            'type_id' => $filter['type_id'],
        );
        $fil = array(
                'time_from' => $last_time,
                'time_to' => date("Y-m-d",$time_from),
        );

        $refuseAnalysisObj = app::get('o2o')->model('store_refuse_analysis');
        $reasons = $refuseAnalysisObj->get_reason_analysis($filter);
            
        $reason_name = array();
        $reason_num = array();
        foreach($reasons as $key => $reason){
            $reason_name[] = '\''.$reason['name'].'\'';
            $reason_num[] = $reason['num'] ? $reason['num'] : 0;    
        }

        $categories = implode(',',$reason_name);
        $volume = implode(',',$reason_num);

        $this->pagedata['categories'] = '['.$categories.']';
        $this->pagedata['data']='{
                name: \'门店拒单原因分析\',
                data: ['.$volume.']
            }';
        $this->display("admin/analysis/ordersPrice/chart_type_column.html");
    }
}